.TH std::basic_string_view::compare 3 "2024.06.10" "http://cppreference.com" "C++ Standard Libary"
.SH NAME
std::basic_string_view::compare \- std::basic_string_view::compare

.SH Synopsis
   constexpr int compare( basic_string_view v ) const noexcept;       \fB(1)\fP \fI(since C++17)\fP
   constexpr int compare( size_type pos1, size_type count1,           \fB(2)\fP \fI(since C++17)\fP
                          basic_string_view v ) const;
   constexpr int compare( size_type pos1, size_type count1,
   basic_string_view v,                                               \fB(3)\fP \fI(since C++17)\fP
                          size_type pos2, size_type count2 ) const;
   constexpr int compare( const CharT* s ) const;                     \fB(4)\fP \fI(since C++17)\fP
   constexpr int compare( size_type pos1, size_type count1,           \fB(5)\fP \fI(since C++17)\fP
                          const CharT* s ) const;
   constexpr int compare( size_type pos1, size_type count1,           \fB(6)\fP \fI(since C++17)\fP
                          const CharT* s, size_type count2 ) const;

   Compares two character sequences.

   1) The length rlen of the sequences to compare is the smaller of size() and
   v.size(). The function compares the two views by calling traits::compare(data(),
   v.data(), rlen), and returns a value according to the following table:

                         Condition                             Result      Return value
   Traits::compare(data(), v.data(), rlen) < 0            this is less     < 0
                                                          than v
                                                size() <  this is less     < 0
                                                v.size()  than v
   Traits::compare(data(), v.data(), rlen) == 0 size() == this is equal to 0
                                                v.size()  v
                                                size() >  this is greater  > 0
                                                v.size()  than v
   Traits::compare(data(), v.data(), rlen) > 0            this is greater  > 0
                                                          than v

   2) Equivalent to substr(pos1, count1).compare(v).
   3) Equivalent to substr(pos1, count1).compare(v.substr(pos2, count2)).
   4) Equivalent to compare(basic_string_view(s)).
   5) Equivalent to substr(pos1, count1).compare(basic_string_view(s)).
   6) Equivalent to substr(pos1, count1).compare(basic_string_view(s, count2)).

.SH Parameters

   v      - view to compare
   s      - pointer to the character string to compare to
   count1 - number of characters of this view to compare
   pos1   - position of the first character in this view to compare
   count2 - number of characters of the given view to compare
   pos2   - position of the first character of the given view to compare

.SH Return value

   Negative value if this view is less than the other character sequence, zero if the
   both character sequences are equal, positive value if this view is greater than the
   other character sequence.

.SH Complexity

   1) Linear in the number of characters compared.

.SH Example


// Run this code

 #include <string_view>

 int main()
 {
     using std::operator""sv;
     static_assert("abc"sv.compare("abcd"sv) < 0);
     static_assert("abcd"sv.compare("abc"sv) > 0);
     static_assert("abc"sv.compare("abc"sv) == 0);
     static_assert(""sv.compare(""sv) == 0);
 }

.SH See also

                      compares two strings
   compare            \fI\fI(public member\fP function of\fP
                      std::basic_string<CharT,Traits,Allocator>)
   operator==
   operator!=
   operator<
   operator>
   operator<=
   operator>=
   operator<=>        lexicographically compares two string views
   \fI(C++17)\fP            \fI(function template)\fP
   (removed in C++20)
   (removed in C++20)
   (removed in C++20)
   (removed in C++20)
   (removed in C++20)
   (C++20)
